﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using Microsoft.Practices.EnterpriseLibrary.Data;
using ChinaDynastyTours.Controls;
using ChinaDynastyTours.Helpers;
using System.Threading;
using IToursDataService;
//using ChinaDynastyTours.Assets;

namespace ChinaDynastyTours.Views
{

    public partial class LookupTeamGroupForm : DevExpress.XtraEditors.XtraForm
    {

        public LookupTeamGroupForm()
        {
            InitializeComponent();
        }
        private void TeamGroupForm_Load(object sender, EventArgs e)
        {
            #region 初始化查询条件，并首次查询

            //默认为上月1日起，下下月1日止
            dateStart.EditValue = DateTime.Parse(string.Format("{0}-{1}-01", DateTime.Now.Month > 1 ? DateTime.Now.Year : DateTime.Now.Year - 1, DateTime.Now.Month > 1 ? DateTime.Now.Month - 1 : 12));
            dateEnd.EditValue = DateTime.Parse(string.Format("{0}-{1}-01", DateTime.Now.Year + DateTime.Now.Month / 11, (DateTime.Now.Month + 1) % 12 + 1));
            
            //初始化部门列表
            //departmentLookupEdit.DataSource = BaseInfoCtrl.GetDepartmentData(0);

            //初始化组团人列表
            ownerALookUpEdit.DataSource = WcfClient.UseService((IToursDataService.IUserInfo u) => (u.GetUserInfoData()));//UserInfoCtrl.GetUserInfoData();

            btnRefresh.PerformClick();

            #endregion

        }

        private void btnSearch_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string guestName = guestSearch.EditValue == null ? "" : guestSearch.EditValue.ToString();
            //string[] providerNameAndID= BaseInfoCtrl.GetProviderNameAndID(Thread.CurrentPrincipal.Identity.Name);
            teamGridControl.DataSource = WcfClient.UseService<ITeamGroupCtrl, DataTable>(t => t.GetTeamGroupData(guestName, int.Parse(ApplicationHelper.CurrentLoginProviderNameAndID[1]))); //TeamGroupCtrl.GetTeamGroupData(guestName, int.Parse(ApplicationHelper.CurrentLoginProviderNameAndID[1]));
        }

        //查询团数据
        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DevExpress.Utils.ToolTipController tip = ApplicationHelper.ShowLoadTip("正在加载数据...", teamGridControl);

            //记住当前选中行‘计划号’
            string focusedTourgroupID = "";
            if (teamGridView.FocusedRowHandle >= 0)
                focusedTourgroupID = teamGridView.GetRowCellValue(teamGridView.FocusedRowHandle, "TourgroupID").ToString();//.FocusedRowHandle;
            //int focusedHandle = teamGridView.FocusedRowHandle;

            //TeamStateEnum teamState = comboStatus.EditValue == null ? TeamStateEnum.全部 : EnumHelper.Team_Str2Enum(comboStatus.EditValue.ToString(), true);
            DateTime dateStart = DateTime.Parse(this.dateStart.EditValue.ToString());
            DateTime dateEnd = DateTime.Parse(this.dateEnd.EditValue.ToString());

            //根据‘组团人’下拉列表过滤
            string ownerA = tbOwnerA.EditValue == null ? "" : tbOwnerA.EditValue.ToString();
            
            //string departmentId="";
            //departmentId = department.EditValue == null ? "" : department.EditValue.ToString();
            //string[] providerNameAndID = BaseInfoCtrl.GetProviderNameAndID(Thread.CurrentPrincipal.Identity.Name);
            this.teamGridControl.DataSource = WcfClient.UseService<ITeamGroupCtrl, DataTable>(t => t.GetTeamGroupData(dateStart, dateEnd, ApplicationHelper.CurrentLoginProviderNameAndID[1], ownerA)); //TeamGroupCtrl.GetTeamGroupData(dateStart, dateEnd, ApplicationHelper.CurrentLoginProviderNameAndID[1], ownerA);
            

            //聚焦刷新前的行
            DataTable dt = teamGridControl.DataSource as DataTable;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["TourgroupID"].ToString().Trim() == focusedTourgroupID.Trim())
                {
                    teamGridView.FocusedRowHandle = teamGridView.GetRowHandle(i);
                    break;
                }
            }
            
            tip.HideHint();
        }


        //团数据行单击事件
        private void teamGridView_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.Clicks == 2)
            {
                Cursor.Current = Cursors.WaitCursor;

                DataTable dt = teamGridControl.DataSource as DataTable;
                dt.DefaultView.RowFilter = string.Format("TourgroupID='{0}'", teamGridView.GetRowCellValue(e.RowHandle, "TourgroupID"));
                TourGroupScheduleForm scheduleForm = new TourGroupScheduleForm(dt.DefaultView);
                //scheduleForm._LookupState = true;
                scheduleForm.ShowDialog(this);
                btnRefresh.PerformClick();

                Cursor.Current = Cursors.Default;
            }
        }
        private void teamGridView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (e.FocusedRowHandle < 0) return;

            string tourgroupId = teamGridView.GetRowCellValue(e.FocusedRowHandle, "TourgroupID").ToString();
        }


        private void teamGridView_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            //不显示状态了
            return;

            /*
            if (e.RowHandle < 0) return;
            object state = teamGridView.GetRowCellValue(e.RowHandle, "TourgroupRowState");

            if (state == null) return;

            switch (state.ToString())
            {
                case "0"://预留
                case "1"://预留
                    break;
                case "2"://此团为已经操作过（手工在计划中设置的）
                    e.Appearance.BackColor = Color.LightSteelBlue;
                    break;
                default:
                    break;
            }
             */
        }
    }
}
